<?php
final class DeviceToMessageModel {
	private $mysql;
	private $tableName = 'DeviceToMessage';
	private $messageTableName = 'Message';
	public $idRowName = 'Message_ID';

/////////////////////////////////
	public $descriptionRowName = 'Message_description';
	public $fromidRowName = 'From_Circle_ID';

	public $fromdevicetocircleIDRowName = 'From_DeviceToCircle_ID';
	public $fromdeviceIDRowName = 'From_Device_ID';
	public $fromstartDateTimeRowName = 'From_DeviceToCircle_StartDateTime';
	public $fromendDateTimeRowName = 'From_DeviceToCircle_EndDateTime';

	public $fromnameRowName = 'From_Circle_Name';
	public $fromtypeRowName = 'From_Circle_Type';

	public $toidRowName = 'To_Circle_ID';
	public $todevicetocircleIDRowName = 'To_DeviceToCircle_ID';
	public $todeviceIDRowName = 'To_Device_ID';
	public $tostartDateTimeRowName = 'To_DeviceToCircle_StartDateTime';
	public $toendDateTimeRowName = 'To_DeviceToCircle_EndDateTime';
	public $tonameRowName = 'To_Circle_Name';
	public $totypeRowName = 'To_Circle_Type';
	public $pubDateRowName = 'Message_pubDate';
/////////////////////////////////
#	private $tableName = 'Message';
#	public $idRowName = 'Message_ID';
#	public $descriptionRowName = 'Message_description';
#	public $fromRowName = 'From_Circle_ID';
#	public $toRowName = 'To_Circle_ID';
#	public $pubDateRowName = 'Message_pubDate';



	public $CountRowName = 'DeviceToMessage_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

	public function GetMessageCount ($id) {
		$ref = 0;
#		printf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`  FROM `%s` WHERE `%s`=%d GROUP BY `%s`;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromidRowName), $this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromdeviceIDRowName), $this->mysql->escape_string ($this->fromstartDateTimeRowName), $this->mysql->escape_string ($this->fromendDateTimeRowName), $this->mysql->escape_string ($this->fromnameRowName), $this->mysql->escape_string ($this->fromtypeRowName), $this->mysql->escape_string ($this->toidRowName), $this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($this->tostartDateTimeRowName), $this->mysql->escape_string ($this->toendDateTimeRowName), $this->mysql->escape_string ($this->tonameRowName), $this->mysql->escape_string ($this->totypeRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), $this->mysql->escape_string ($this->idRowName));;
#		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`  FROM `%s` WHERE `%s`=%d GROUP BY `%s`;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromidRowName), $this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromdeviceIDRowName), $this->mysql->escape_string ($this->fromstartDateTimeRowName), $this->mysql->escape_string ($this->fromendDateTimeRowName), $this->mysql->escape_string ($this->fromnameRowName), $this->mysql->escape_string ($this->fromtypeRowName), $this->mysql->escape_string ($this->toidRowName), $this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($this->tostartDateTimeRowName), $this->mysql->escape_string ($this->toendDateTimeRowName), $this->mysql->escape_string ($this->tonameRowName), $this->mysql->escape_string ($this->totypeRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), $this->mysql->escape_string ($this->idRowName)));
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM (SELECT * FROM `%s` WHERE `%s`=%d AND `%s` IS NOT NULL GROUP BY `%s`) AS `TEMP`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), $this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->idRowName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}
/*
	public function GetMessageCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}*/

	public function GetMessageItems ($id, $offset, $count, $toCircleID) {
		$ref = array ();

		if($toCircleID){
			$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`  
				FROM `%s` WHERE `%s`=%d AND `%s` IS NOT NULL AND `%s`=%d GROUP BY `%s`;', 
				$this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromidRowName), 
				$this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromdeviceIDRowName), 
				$this->mysql->escape_string ($this->fromstartDateTimeRowName), $this->mysql->escape_string ($this->fromendDateTimeRowName), 
				$this->mysql->escape_string ($this->fromnameRowName), $this->mysql->escape_string ($this->fromtypeRowName), $this->mysql->escape_string ($this->toidRowName), 
				$this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->todeviceIDRowName), 
				$this->mysql->escape_string ($this->tostartDateTimeRowName), $this->mysql->escape_string ($this->toendDateTimeRowName), 
				$this->mysql->escape_string ($this->tonameRowName), $this->mysql->escape_string ($this->totypeRowName), $this->mysql->escape_string ($this->pubDateRowName), 
				$this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), 
				$this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromidRowName), $toCircleID,

				$this->mysql->escape_string ($this->idRowName)));
		}else{
			$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`  
				FROM `%s` WHERE `%s`=%d AND `%s` IS NOT NULL GROUP BY `%s`;', 
				$this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromidRowName), 
				$this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromdeviceIDRowName), 
				$this->mysql->escape_string ($this->fromstartDateTimeRowName), $this->mysql->escape_string ($this->fromendDateTimeRowName), 
				$this->mysql->escape_string ($this->fromnameRowName), $this->mysql->escape_string ($this->fromtypeRowName), $this->mysql->escape_string ($this->toidRowName), 
				$this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->todeviceIDRowName), 
				$this->mysql->escape_string ($this->tostartDateTimeRowName), $this->mysql->escape_string ($this->toendDateTimeRowName), 
				$this->mysql->escape_string ($this->tonameRowName), $this->mysql->escape_string ($this->totypeRowName), $this->mysql->escape_string ($this->pubDateRowName), 
				$this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), 
				$this->mysql->escape_string ($this->fromdevicetocircleIDRowName), 
				$this->mysql->escape_string ($this->idRowName)));			
		}

#printf ('SELECT `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`  FROM `%s` WHERE `%s`=%d GROUP BY `%s`;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromidRowName), $this->mysql->escape_string ($this->fromdevicetocircleIDRowName), $this->mysql->escape_string ($this->fromdeviceIDRowName), $this->mysql->escape_string ($this->fromstartDateTimeRowName), $this->mysql->escape_string ($this->fromendDateTimeRowName), $this->mysql->escape_string ($this->fromnameRowName), $this->mysql->escape_string ($this->fromtypeRowName), $this->mysql->escape_string ($this->toidRowName), $this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($this->tostartDateTimeRowName), $this->mysql->escape_string ($this->toendDateTimeRowName), $this->mysql->escape_string ($this->tonameRowName), $this->mysql->escape_string ($this->totypeRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todeviceIDRowName), $this->mysql->escape_string ($id), $this->mysql->escape_string ($this->idRowName));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function CleanUPMessage () {
		$sqlresult = $this->mysql->query (sprintf ('DELETE `%s`.* FROM `%s` JOIN `%s` USING (`%s`) WHERE `%s`.`%s` IS NULL OR `%s`.`%s` IS NULL;', $this->mysql->escape_string ($this->messageTableName), $this->mysql->escape_string ($this->messageTableName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->todevicetocircleIDRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->fromdevicetocircleIDRowName)));
		$sqlresult->free_result ();
	}

/*
	public function DelMessageItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}

	public function AddMessageItem ($description) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`) VALUES (now(), \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($description)));
		$sqlresult->free_result ();
	}
*/
}
?>
